package com.syp.hehe.web.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.syp.hehe.domain.ArticleTypeEnum;
import com.syp.hehe.domain.vo.ArticleVo;
import com.syp.hehe.service.ArticleServiceI;
import com.syp.hehe.util.Constants;
import com.syp.hehe.util.ProcessResult;

@Controller
@RequestMapping("/article")
public class ArticleController {

	private static final Logger logger = LoggerFactory.getLogger(ArticleController.class);

	@Autowired
	private ArticleServiceI articleService;

	@RequestMapping("/list")
	public ModelAndView list() {
		List<ArticleVo> articleList = this.articleService.getPageList(null, 1, Constants.ARTICLE_PIGE_SIZE);

		ModelAndView mv = new ModelAndView("/article/list");
		mv.addObject("articleList", articleList);
		return mv;
	}

	@RequestMapping("/list/{type}")
	public ModelAndView list(@PathVariable(value = "type") String articleType) {
		Integer type = articleType == null ? null : ArticleTypeEnum.getEnum(articleType).getCode();

		List<ArticleVo> articleList = this.articleService.getPageList(type, 1, Constants.ARTICLE_PIGE_SIZE);

		ModelAndView mv = new ModelAndView("/article/list");
		mv.addObject("articleList", articleList);
		return mv;
	}

	@RequestMapping("/list/{type}/{pageNum}")
	public ModelAndView list(@PathVariable(value = "type") String type, @PathVariable("pageNum") Integer pageNum) {
		pageNum = pageNum == null ? 1 : pageNum;
		Integer articleType = type == null ? null : ArticleTypeEnum.getEnum(type).getCode();

		List<ArticleVo> articleList = this.articleService.getPageList(articleType, pageNum, Constants.ARTICLE_PIGE_SIZE);

		ModelAndView mv = new ModelAndView("/article/list");
		mv.addObject("articleList", articleList);
		return mv;
	}

	@RequestMapping("/publish")
	public String publish() {
		return "/article/publish";
	}

	@RequestMapping(value = "/dpublish")
	public ModelAndView dpublish(@RequestParam MultipartFile pic, ArticleVo article) {
		ProcessResult pr = new ProcessResult();
		ModelAndView mv = null;
		try {
			this.articleService.insert(article);

			mv = new ModelAndView("/article/publish.result");
			mv.addObject("result", pr);
			pr.setSuccess(true);
			pr.setMessage("投稿成功。");
		} catch (Exception e) {
			logger.error("投稿失败。", e);

			pr.setSuccess(false);
			pr.setMessage("投稿失败。");
			mv = new ModelAndView("/system/500");
			mv.addObject("result", pr);
		}
		return mv;
	}
	
	@RequestMapping(value = "/img", method = RequestMethod.GET)
	public void articleImg(Long id, HttpServletResponse response) {
		OutputStream out = null;

		try {
			byte[] picBytes = this.articleService.getPicBytes(id);

			out = new BufferedOutputStream(response.getOutputStream());
			out.write(picBytes);

		} catch (Exception e) {
			logger.error("图片获取失败。", e);
			throw new RuntimeException(e);
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
